import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../widget/ntt_gesture_widget.dart';
import '../widget/ntt_ordinary_button.dart';

class NttInputDialog extends StatelessWidget {
  String TAG = "[NttInputDialog]";

  /// 标题
  final String? title;

  /// 输入控制器
  final TextEditingController _controller = TextEditingController();

  /// 输入框焦点
  final FocusNode _inputFocusNode = FocusNode();

  /// 左侧按钮文本
  String? leftBtnTxt;

  /// 右侧按钮文本
  String? rightBtnTxt;

  /// 提示文本
  String? hintText;

  /// 输入框类型
  TextInputType inputType;

  /// 输入框动作类型
  TextInputAction textInputAction;

  /// 是否需要点击空白区域关闭弹窗
  bool barrierDismissible;

  bool resizeToAvoidBottomInset;

  int? maxLength;

  NttInputDialog({
    super.key,
    this.title,
    this.leftBtnTxt,
    this.rightBtnTxt,
    this.hintText,
    this.inputType = TextInputType.visiblePassword,
    this.textInputAction = TextInputAction.search,
    this.barrierDismissible = true,
    this.resizeToAvoidBottomInset = true,
    this.maxLength,
    String? text,
  }) {
    if(text != null){
      _controller.text = text;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset:resizeToAvoidBottomInset,
      backgroundColor: Colors.transparent,
      body: PopScope(
        onPopInvoked: (didPop) {
          if (!didPop) {
            if (_inputFocusNode.hasFocus) {
              hide();
            } else {
              if(barrierDismissible){
                Navigator.of(context).pop(false);
              }
            }
          }
        },
        canPop: false,
        child: NttGestureWidget(
          behavior: HitTestBehavior.opaque,
          onTap: () {
            if (_inputFocusNode.hasFocus) {
              hide();
            } else {
              if(barrierDismissible){
                Navigator.of(context).pop(false);
              }
            }
          },
          child: Center(
            child: Container(
              width: 640.w,
              constraints: BoxConstraints(maxHeight: 0.7.sh),
              height: 416.h,
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(56.h),
              ),
              padding: EdgeInsets.only(
                  top: 30.h, bottom: 32.h, left: 32.w, right: 32.w),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisSize: MainAxisSize.min,
                children: [
                  Text(
                    title ?? "标题",
                    style: TextStyle(
                        fontSize: 44.h, color: const Color(0xff40423E)),
                  ),
                  SizedBox(
                    height: 24.h,
                  ),
                  Container(
                    height: 120.h,
                    decoration: BoxDecoration(
                      color: const Color(0xffF4F6FA),
                      borderRadius: BorderRadius.circular(16.w),
                    ),
                    padding: EdgeInsets.symmetric(horizontal: 10.w),
                    child: TextField(
                      controller: _controller,
                      autofocus: true,
                      onChanged: (text) {
                        debugPrint('$TAG , 当前输入文本为：$text');
                      },
                      onSubmitted: (text) {
                        debugPrint('$TAG , 回车时文本为：$text');
                        hide();
                        Navigator.of(context).pop({"text": text});
                      },
                      onTap: () {
                        _controller.selection = TextSelection.fromPosition(
                          TextPosition(offset: _controller.text.length),
                        );
                      },
                      // 禁用长按效果
                      enableInteractiveSelection: false,
                      // style: const TextStyle(color: Colors.transparent),
                      // // 将文本颜色设置为透明
                      // showCursor: false,
                      // // 隐藏光标
                      decoration: InputDecoration(
                        // 隐藏边框
                        border: InputBorder.none,
                        // 不填充背景色
                        filled: false,
                        // 设置提示文本颜色
                        hintStyle: TextStyle(
                            color: const Color(0xffA4A8B2),
                            fontSize: 36.r,
                            fontWeight: FontWeight.w500),
                        hintText: ' $hintText',
                        counterText:"",
                      ),
                      maxLength: maxLength,
                      style: TextStyle(
                          color: const Color(0xff495166),
                          fontSize: 36.r,
                          fontWeight: FontWeight.w600),
                      keyboardType: inputType,
                      textInputAction: textInputAction,
                      maxLengthEnforcement:
                          MaxLengthEnforcement.truncateAfterCompositionEnds,
                      focusNode: _inputFocusNode,
                    ),
                  ),
                  const Expanded(child: SizedBox()),
                  SizedBox(
                    height: 24.h,
                  ),
                  Row(
                    children: [
                      // Expanded(
                      //   child: NttGestureWidget(
                      //     onTap: () {
                      //       Navigator.of(context).pop(false);
                      //     },
                      //     child: Container(
                      //       width: 280.w,
                      //       height: 96.h,
                      //       decoration: BoxDecoration(
                      //         color: Colors.white,
                      //         borderRadius: BorderRadius.circular(100.h),
                      //         border: Border.all(
                      //             color: const Color(0xffFDAD30), width: 4.h),
                      //       ),
                      //       child: Center(
                      //         child: Text(
                      //           leftBtnTxt ?? "取消",
                      //           style: TextStyle(
                      //               fontSize: 36.r,
                      //               color: const Color(0xffFDAD30)),
                      //         ),
                      //       ),
                      //     ),
                      //   ),
                      // ),
                      Expanded(
                        child: NttOrdinaryButton(
                          leftBtnTxt ?? "取消",
                          width: 280.w,
                          height: 96.h,
                          textSize: 36.r,
                          textColor: const Color(0xff34443A).withOpacity(0.6),
                          isShowBorder: false,
                          radius: 16.w,
                          backgroundColor: const Color(0xffF4F6FA),
                          onTap: () {
                            Navigator.of(context).pop(false);
                          },
                        ),
                      ),
                      SizedBox(
                        width: 16.w,
                      ),
                      Expanded(
                        child: NttOrdinaryButton(
                          rightBtnTxt ?? "确定",
                          width: 280.w,
                          height: 96.h,
                          textSize: 36.r,
                          textColor: Colors.white,
                          isShowBorder: false,
                          radius: 16.w,
                          backgroundColor: const Color(0xffFF9B25),
                          onTap: () {
                            Navigator.of(context).pop({"text": _controller.text});
                          },
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  /// 隐藏软键盘
  void hide() {
    _inputFocusNode.unfocus();
  }
}
